package com.wisdom.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.bluecall.sipua.R;

public class DialSettingActivity extends Activity {
	RadioButton callbackRadio;
	RadioButton directcallRadio;
	RadioButton wisdomCallRadio;

	public static final String SHAREDPREFERENCE_DB = "dital_setting";
	public static final String SHAREDPREFERENCE_CALLSETTING = "callsetting";
	public static final int SHAREDPREFERENCE_CALLBACK = 0;
	public static final int SHAREDPREFERENCE_DIRECTCALL = 1;
	public static final int SHAREDPREFERENCE_EACHTIME = 2;
	public static final int SHAREDPREFERENCE_WISTOMCALL = 3;
	private static int CURRENT_CHOOSE;
	private SharedPreferences.Editor editor;
	SharedPreferences sharedPreference;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_dialsetting);
		sharedPreference = getSharedPreferences(SHAREDPREFERENCE_DB, MODE_PRIVATE);
		initResourceRefs();

		// 根据ID找到RadioGroup实例
		RadioGroup group = (RadioGroup) this
				.findViewById(R.id.radioGroupcallsetting);
		// 绑定一个匿名监听器
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {

				editor = sharedPreference.edit();
				// TODO Auto-generated method stub
				// 获取变更后的选中项的ID
				int radioButtonId = arg0.getCheckedRadioButtonId();
				switch (radioButtonId) {
				case R.id.ditalsetting_callback:
					CURRENT_CHOOSE = SHAREDPREFERENCE_CALLBACK;
					break;
				case R.id.ditalsetting_directcall:
					CURRENT_CHOOSE = SHAREDPREFERENCE_DIRECTCALL;
					break;
				case R.id.ditalsetting_title_eachtime:
					CURRENT_CHOOSE = SHAREDPREFERENCE_EACHTIME;
					break;
				case R.id.ditalsetting_wisdomcall:
					CURRENT_CHOOSE = SHAREDPREFERENCE_WISTOMCALL;
					break;
				default:
					break;
				}
				editor.putInt(SHAREDPREFERENCE_CALLSETTING,
						CURRENT_CHOOSE);
				editor.commit();

			}
		});
	}

	void initResourceRefs() {
		switch (sharedPreference.getInt(SHAREDPREFERENCE_CALLSETTING, SHAREDPREFERENCE_WISTOMCALL)) {
		case SHAREDPREFERENCE_CALLBACK:
			((RadioButton) this.findViewById(R.id.ditalsetting_callback))
					.setChecked(true);
			break;
		case SHAREDPREFERENCE_DIRECTCALL:
			((RadioButton) this.findViewById(R.id.ditalsetting_directcall))
					.setChecked(true);
			break;
		case SHAREDPREFERENCE_EACHTIME:
			((RadioButton) this.findViewById(R.id.ditalsetting_title_eachtime))
					.setChecked(true);
			break;
		case SHAREDPREFERENCE_WISTOMCALL:
			((RadioButton) this.findViewById(R.id.ditalsetting_wisdomcall))
					.setChecked(true);
			break;
		default:
			break;
		}
	}
}
